package com.bugpool.leilema.tc.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bugpool.leilema.tc.bean.entity.SysUserRole;
import org.apache.ibatis.annotations.Mapper;


import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author wh
 * @Description 用户角色表
 * @Date 2023/6/12 16:21
 * @Version 1.0
 **/
@Mapper
public interface UserRoleMapper extends BaseMapper<SysUserRole> {

    /**
     * 根据用户id 获取所有的角色id集合
     *
     * @param userId
     * @return
     */
    default List<Long> getRoleIdsByUserId(Long userId) {
        QueryWrapper<SysUserRole> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SysUserRole::getUserId, userId);
        return this.selectList(wrapper).stream().map(SysUserRole::getRoleId).distinct().collect(Collectors.toList());
    }

    /**
     * 根据角色id 判断 角色下面是否关联着用户
     *
     * @param ids
     * @return
     */
    default Integer getUserCountIdByRoleIds(List<Long> roleIds) {
        QueryWrapper<SysUserRole> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(SysUserRole::getRoleId, roleIds);
        return this.selectCount(wrapper);
    }

    /**
     * 根据角色id 获取用户id集合
     *
     * @param roleId
     * @return
     */
    default List<Long> getUserIdsByRoleId(Long roleId) {
        QueryWrapper<SysUserRole> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SysUserRole::getRoleId,roleId);
        return this.selectList(wrapper).stream().map(SysUserRole::getUserId).distinct().collect(Collectors.toList());
    }
}
